<template>
  <el-card shadow="never" body-style="padding: 14px 5px 0 0;">
    <div slot="header" class="flex-between">
      <span class="titleCard">数据统计</span>
      <div>
        <div
          :class="['ele-inline-block', { 'hidden-xs-only': styleResponsive }]"
        >
          <el-radio-group v-model="saleSearch.dateType" size="small">
            <el-radio-button :label="0">今天</el-radio-button>
            <el-radio-button :label="1">本周</el-radio-button>
            <el-radio-button :label="2">本月</el-radio-button>
            <el-radio-button :label="3">本年</el-radio-button>
          </el-radio-group>
        </div>
        <div
          :class="[
            'ele-inline-block',
            { 'hidden-sm-and-down': styleResponsive }
          ]"
          style="width: 260px; margin-left: 10px"
        >
          <el-date-picker
            unlink-panels
            type="daterange"
            class="ele-fluid"
            :default-time="['00:00:00', '23:59:59']"
            value-format="yyyy-MM-ddTHH:mm:ss"
            end-placeholder="结束日期"
            start-placeholder="开始日期"
            v-model="saleSearch.datetime"
            range-separator="至"
            size="small"
          />
        </div>
      </div>
    </div>
    <v-chart
      ref="visitHourChart"
      style="height: 323px"
      :option="visitHourChartOption"
    />
  </el-card>
</template>

<script>
  import { use } from 'echarts/core';
  import { CanvasRenderer } from 'echarts/renderers';
  import { LineChart } from 'echarts/charts';
  import {
    GridComponent,
    TooltipComponent,
    LegendComponent
  } from 'echarts/components';
  import VChart from 'vue-echarts';
  import { getVisitHourList } from '@/api/dashboard/analysis';
  import { echartsMixin } from '@/utils/echarts-mixin';

  use([
    CanvasRenderer,
    LineChart,
    GridComponent,
    TooltipComponent,
    LegendComponent
  ]);

  export default {
    components: { VChart },
    mixins: [echartsMixin(['visitHourChart'])],
    data() {
      return {
        // 最近1小时访问情况折线图配置
        visitHourChartOption: {},
        saleSearch: {
          datetime: ''
        }
      };
    },
    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },
    created() {
      this.getVisitHourData();
    },
    methods: {
      /* 获取最近1小时访问情况数据 */
      getVisitHourData() {
        getVisitHourList()
          .then((data) => {
            this.visitHourChartOption = {
              tooltip: {
                trigger: 'axis'
              },
              legend: {
                data: ['浏览量', '访问量'],
                right: 20
              },
              xAxis: [
                {
                  type: 'category',
                  boundaryGap: false,
                  data: data.map((d) => d.time)
                }
              ],
              yAxis: [
                {
                  type: 'value'
                }
              ],
              series: [
                {
                  name: '浏览量',
                  type: 'line',
                  smooth: true,
                  symbol: 'none',
                  areaStyle: {
                    opacity: 0.5
                  },
                  data: data.map((d) => d.views)
                },
                {
                  name: '访问量',
                  type: 'line',
                  smooth: true,
                  symbol: 'none',
                  areaStyle: {
                    opacity: 0.5
                  },
                  data: data.map((d) => d.visits)
                }
              ]
            };
          })
          .catch((e) => {
            this.$message.error(e.message);
          });
      }
    }
  };
</script>
<style lang="less" scoped>
  .titleCard {
    &::before {
      content: url('../../../../static/svg/userStatistics.svg');
      display: inline-block;
      vertical-align: middle;
      margin-right: 2px;
      padding-left: 9px;
    }
  }
</style>
